package linter.linterjava.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import linter.linterjava.POJO.DO.*;
import linter.linterjava.POJO.DTO.ArticleReviewDTO;
import linter.linterjava.POJO.UtilDTO.PageInfoDTO;
import linter.linterjava.custom.ConstantPublicWay;
import linter.linterjava.mapper.*;
import linter.linterjava.service.ArticleReviewService;
import linter.linterjava.utils.PropertyUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static linter.linterjava.custom.Constant.NOTE_CONSTANT_INITIAL_VALUE_DEFAULT;
import static linter.linterjava.custom.CustomResultType.COMMENT_ERROR;
import static linter.linterjava.custom.CustomResultType.CREATION_SUCCESS;

/**
* @author 北煜
* @description 针对表【article_review(文章评论表)】的数据库操作Service实现
* @createDate 2024-07-07 12:15:05
*/
@Service
public class ArticleReviewServiceImpl extends ServiceImpl<ArticleReviewMapper, ArticleReview>
    implements ArticleReviewService{

    @Resource
    ArticleReviewMapper articleReviewMapper;
    @Resource
    ArticleMapper articleMapper;
    @Resource
    ReviewLikesMapper reviewLikesMapper;
    @Resource
    ReviewReportMapper reviewReportMapper;
    @Resource
    ReviewDislikesMapper reviewDislikesMapper;

    // TODO: 没有实现评论评论。设计的树形结构还需要继续设计

    @Override
    public String addComment(Long uuid, ArticleReviewDTO articleReviewDTO) {
        Date date = new Date();
        ArticleReview articleReview = new ArticleReview();
        articleReview.setArticleId(articleReviewDTO.getArticleId());
        articleReview.setParentReview(articleReviewDTO.getParentReview());
        articleReview.setReviewContent(articleReviewDTO.getReviewContent());
        articleReview.setUserId(uuid);
        articleReview.setCreateTime(date);
        articleReview.setUpdataTime(date);
        articleReview.setReviewLikeNub(NOTE_CONSTANT_INITIAL_VALUE_DEFAULT);
        articleReview.setReviewDislikeNub(NOTE_CONSTANT_INITIAL_VALUE_DEFAULT);
        articleReview.setReviewCommentNub(NOTE_CONSTANT_INITIAL_VALUE_DEFAULT);
        int flag = articleReviewMapper.insert(articleReview);
        Article article = new Article();
        Integer reviewNub = articleMapper.selectById(articleReviewDTO.getArticleId()).getCollectNub();
        article.setReviewNub(reviewNub + 1);
        article.setUpdataTime(date);
        flag += articleMapper.update(article, new QueryWrapper<Article>().eq("id", articleReviewDTO.getArticleId()));
//        ReviewLikes reviewLikes = new ReviewLikes();
//        reviewLikes.setReviewId(articleReview.getId());
//        reviewLikes.setUserId(uuid);
//        reviewLikes.setCreateTime(date);
//        reviewLikes.setUpdataTime(date);
//        flag += reviewLikesMapper.insert(reviewLikes);
//        ReviewDislikes reviewDislikes = new ReviewDislikes();
//        reviewDislikes.setReviewId(articleReview.getId());
//        reviewDislikes.setUserId(uuid);
//        reviewDislikes.setCreateTime(date);
//        reviewDislikes.setUpdataTime(date);
//        flag += reviewDislikesMapper.insert(reviewDislikes);
//        ReviewReport reviewReport = new ReviewReport();
//        reviewReport.setReviewId(articleReview.getId());
//        reviewReport.setUserId(uuid);
//        reviewReport.setCreateTime(date);
//        reviewReport.setUpdataTime(date);
//        flag += reviewReportMapper.insert(reviewReport);
        return flag == 2 ? CREATION_SUCCESS : COMMENT_ERROR;
    }

    @Override
    public PageInfo<ArticleReview> getReview(Long uuid, ArticleReviewDTO articleReviewDTO) {
        PageInfoDTO pageInfoDTO = new PageInfoDTO();
        pageInfoDTO.setPageNum(articleReviewDTO.getPageNum());
        pageInfoDTO.setPageSize(articleReviewDTO.getPageSize());
        PageInfo<ArticleReview> pageInfo = ConstantPublicWay.getPageInfo(pageInfoDTO, new QueryWrapper<ArticleReview>()
                .eq("article_id", articleReviewDTO.getArticleId()).eq("is_delete", 0), articleReviewMapper);
        List<ArticleReview> list = pageInfo.getList();
        return new PageInfo<>(list);
    }

    @Override
    public String reviewComment(Long uuid, ArticleReviewDTO articleReviewDTO) {
        Date date = new Date();
        ArticleReview articleReview = new ArticleReview();
        articleReview.setArticleId(articleReviewDTO.getArticleId());
        articleReview.setParentReview(articleReviewDTO.getParentReview());
        articleReview.setReviewContent(articleReviewDTO.getReviewContent());
        articleReview.setUserId(uuid);
        articleReview.setCreateTime(date);
        articleReview.setUpdataTime(date);
        articleReview.setReviewLikeNub(NOTE_CONSTANT_INITIAL_VALUE_DEFAULT);
        articleReview.setReviewDislikeNub(NOTE_CONSTANT_INITIAL_VALUE_DEFAULT);
        articleReview.setReviewCommentNub(NOTE_CONSTANT_INITIAL_VALUE_DEFAULT);
        int flag = articleReviewMapper.insert(articleReview);
        // 被回复的评论
        ArticleReview commentReview = new ArticleReview();
        Integer reviewCommentNub = articleReviewMapper.selectById(articleReviewDTO.getArticleId()).getReviewCommentNub();
        commentReview.setReviewCommentNub(reviewCommentNub + 1);
        flag += articleReviewMapper.update(commentReview, new QueryWrapper<ArticleReview>()
                .eq("id", articleReviewDTO.getParentReview()).eq("is_delete", 0));
        return flag == 2 ? CREATION_SUCCESS : COMMENT_ERROR;
    }

}




